package com.school.demo.controller.api;

import com.school.demo.model.auto.School;
import com.school.demo.service.ISchoolService;
import com.school.demo.util.Restful;
import com.school.demo.util.Status;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: demo
 * @description: 学校api
 * @author: Charlotte
 * @create: 2021-03-21 03:07
 **/
@Api(tags = "学校管理API")
@RequestMapping("/api/school")
@Controller
public class SchoolApiController {

    @Autowired
    ISchoolService schoolService;


    /**
     * 获取数据库所有学校信息（名字，id）比如：name：河北师范大学 id：1
     */
    @ApiOperation(value = "获取数据库所有学校信息", notes = "", tags = {})
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> list() throws Exception {

        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("data",schoolService.list());
        return map;
    }

    /**TODO
     * 根据学校id，返回学校专业列表（专业列表是个json字符串）
     */
    @ApiOperation(value = "根据学校id获取学校信息", notes = "", tags = {})
    @RequestMapping(value = "/getById",method = RequestMethod.POST)
    public @ResponseBody
    Restful getById(@RequestParam(name = "id",defaultValue = "1")String id) throws Exception {

        if (id.isEmpty()){
            return new Restful(Status.REQUEST_PARAMETER_ERROR);
        }
        try {
            School school = schoolService.getById(id);
            if (school == null){
                Restful restful = new Restful();
                restful.setCode(Status.WARN.code);
                restful.setMsg("此id的学校不存在");
                return restful;
            }
            Restful restful = new Restful(Status.SUCCESS);
            restful.setData(school);
            return restful;
        }catch (Exception e){
            Restful restful = new Restful(Status.SYSTEM_ERROR);
            return restful;
        }


    }
}
